AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

function ENT:Initialize()
	self.Entity:SetModel("models/Items/item_item_crate.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	local phys = self.Entity:GetPhysicsObject()
	if phys and phys:IsValid() then phys:Wake() end

	self.Entity:SetUseType(SIMPLE_USE)

	self.HP = 100
end

function ENT:Fill( class, model, quantity )
	self.Shipment = {}
	self.Shipment.Class = class
	self.Shipment.Model = model
	self.Shipment.Quantity = quantity
end

function ENT:Use( activator, caller )
	self:SpawnEntity()
end

function ENT:SpawnEntity()
	self.Shipment.Quantity = self.Shipment.Quantity - 1
	local Entity = ents.Create(self.Shipment.Class)
	Entity:SetPos(self:GetPos() + Vector(0,0,30))
	Entity:Spawn()

	if self.Shipment.Quantity == 0 then
		self.Entity:Remove()
	end
end

function ENT:OnTakeDamage(dmg)
	self.Entity:TakePhysicsDamage(dmg)
        self.HP = self.HP - dmg:GetDamage()
        if self.HP <= 0 then
                self.Entity:Destroy()
        end
end

function ENT:Destroy()
	while self.Shipment.Quantity >= 1 do
		self:SpawnEntity()
	end	

	self.Entity:Remove()
end